package week2.day12;

/**
 * @Author fengzihao
 * @Date 2024-04-26 20:15
 */
public class TestIComputer {


    public static void main(String[] args) {
        // 实现类
        IComputer iComputer1 = new ComputerImpl();

        // 局部内部类
        IComputer iComputer2 = getComputerImpl();

        IComputer iComputer3 = new IComputer() {
            @Override
            public double plus(double x, double y) {
                return x+y;
            }
            @Override
            public double minus(double x, double y) {
                return x-y;
            }

            @Override
            public double times(double x, double y) {
                return x*y;
            }

            @Override
            public double divide(double x, double y) {
                return x/y;
            }
        };

        System.out.println("iComputer1.plus(5,2) = " + iComputer1.plus(5, 2));
        System.out.println("iComputer2.minus(4,1) = " + iComputer2.minus(4, 1));
        System.out.println("iComputer3.times(3,5) = " + iComputer3.times(3, 5));
        System.out.println("iComputer3.divide(6,2) = " + iComputer3.divide(6, 2));


    }
    public static IComputer getComputerImpl(){
        class IComputerImpl implements IComputer{

            @Override
            public double plus(double x, double y) {
                return x+y;
            }

            @Override
            public double minus(double x, double y) {
                return x-y;
            }

            @Override
            public double times(double x, double y) {
                return x*y;
            }

            @Override
            public double divide(double x, double y) {
                return x/y;
            }
        }
        return new IComputerImpl();
    }
}

interface IComputer{
    double plus(double x,double y);
    double minus(double x,double y);
    double times(double x,double y);
    double divide(double x,double y);
}
class ComputerImpl implements IComputer{

    @Override
    public double plus(double x, double y) {
        return x+y;
    }

    @Override
    public double minus(double x, double y) {
        return x-y;
    }

    @Override
    public double times(double x, double y) {
        return x*y;
    }

    @Override
    public double divide(double x, double y) {
        return x/y;
    }
}
